package com.geo.geoProject.client.UI.docks;

import com.geo.geoProject.client.application.Programme;
import com.geo.geoProject.client.donneesStatiques.ApplicationGestion;
import com.geo.geoProject.client.ecrans.geoLove.EcranGeoLove;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DropEvent;
import com.smartgwt.client.widgets.events.DropHandler;
import com.smartgwt.client.widgets.events.DropOutEvent;
import com.smartgwt.client.widgets.events.DropOutHandler;


public class Docks implements DropHandler, ResizeHandler, DropOutHandler {

	///////////////////////////////////////
	////////////// SINGLETON //////////////
	///////////////////////////////////////
	private static Docks docs = new Docks() ;
	private static DockBas bas;
	private static DockDroite droite;
	private Docks() {}
	public static Docks get() {return docs ;}
	///////////////////////////////////////
	///////////////////////////////////////
	///////////////////////////////////////
	
	// uniquement un Dock en bas !!!
	private static final boolean QUEBAS = true ;
	private static boolean dejasAddWindowHandlet;
	
	public static void renderDocks(final EcranGeoLove ecranGeoLove) {
		if (bas==null) {
			bas = new DockBas() ;
//			bas.addMember(new Icone("cube_blue.png")) ;
//			bas.addMember(new Icone("cube_blue.png")) ;

//			bas.addMember(new Icone("exit.png", new Runnable() {
//				public void run() { 
//					ecranGeoLove.getEcouteur().logout() ;
//				}
//			})) ;
			
			bas   .addDropOutHandler(get()) ;
			ApplicationGestion.get().canvasGlobal.addChild(bas   );  
		}
		
		
		if (!QUEBAS) {
			if (droite==null) {
				droite = new DockDroite() ;
				droite.addMember(new Icone("cube_blue.png")) ;
				droite.addMember(new Icone("cube_blue.png")) ;
				droite.addMember(new Icone("cube_blue.png")) ;
				
				droite.setVisible(true) ;
				droite.addDropOutHandler(get()) ;
				ApplicationGestion.get().canvasGlobal.addChild(droite);  
			}
		}

		if (bas!=null) {
			bas.setVisible(true) ;
		}
		if (droite!=null) {
			droite.setVisible(true) ;
		}

		if (!dejasAddWindowHandlet) {
			ApplicationGestion.addWindowHandlet(get()) ;
			dejasAddWindowHandlet = true ;
		}
		get().resizeNow() ;
		
		
		ApplicationGestion.get().canvasGlobal.setBackgroundImage(GWT.getModuleBaseURL()+"/dock/body-bg.gif") ;
	}
	public static void hideDocks(EcranGeoLove ecranGeoLove) {
		if (bas!=null) {
			bas.setVisible(false) ;
		}
		if (droite!=null) {
			droite.setVisible(false) ;
		}
	}

	public void onDrop(DropEvent event) {
		resize() ;
	}
	public void onResize(ResizeEvent event) {
		resize() ;
	}
	@Override
	public void onDropOut(DropOutEvent event) {
		resize() ;
	}
	
	private void resize() {
		ApplicationGestion.runScriptOnce(new Runnable() {
			public void run() {
				if (Docks.bas!=null) {
					int w = (Docks.bas.getRight()-Docks.bas.getLeft()) ;
					Docks.bas.moveTo(Window.getClientWidth()/2-w/2, Window.getClientHeight()-120) ;
				}
				if (Docks.droite!=null) {
					int h = (Docks.droite.getBottom()-Docks.droite.getTop()) ;
					Docks.droite.moveTo(Window.getClientWidth()-120, Window.getClientHeight()/2-h/2) ;
				}
			}
		}, Random.nextInt(150)) ;
	}
	private void resizeNow() {
		if (Docks.bas!=null) {
			int w = (Docks.bas.getRight()-Docks.bas.getLeft()) ;
			Docks.bas.moveTo(Window.getClientWidth()/2-w/2, Window.getClientHeight()-120) ;
		}
		if (Docks.droite!=null) {
			int h = (Docks.droite.getBottom()-Docks.droite.getTop()) ;
			Docks.droite.moveTo(Window.getClientWidth()-120, Window.getClientHeight()/2-h/2) ;
		}
	}
	public static void addProgramme(Icone i, final Programme p) {
		bas.addMember(i) ;
		i.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				p.clickIcone() ;
			}
		}) ;
	}
}
